// pages/address/index.js
import { areaList } from '@vant/area-data';
import {request,fail,ok} from "../../util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:undefined,
    phone:undefined,
    detail:undefined,
    area:[],
    ifDefault:false,
    areaList:areaList,
    areaShow:false,
    areaChoose:undefined,
    addressId:undefined
  },
  onAreaShow(){
    this.setData({areaShow:!this.data.areaShow})
  },
  confirmArea(e){
    //console.log(e);
    this.setData({areaChoose:e.detail.values})
    this.setData({areaShow:!this.data.areaShow})
  },
  setToDefault(){
    this.setData({ifDefault:!this.data.ifDefault})
  },
  onSave(){
    if(this.data.addressId){
      request({
        url:"/address/edite",
        method:"POST",
        data:{
          addressId:this.data.addressId,
          name:this.data.name,
          phone:this.data.phone,
          ifDefault:this.data.ifDefault,
          detail:this.data.detail,
          area:this.data.areaChoose,
          location:
          this.data.areaChoose[0].name+''+this.data.areaChoose[1].name+''+this.data.areaChoose[2].name+this.data.detail
        }
      }).then(res=>{
        wx.navigateBack()
      })
    }else{
      request({
        url:"/address/add",
        method:"POST",
        data:{
          name:this.data.name,
          phone:this.data.phone,
          ifDefault:this.data.ifDefault,
          detail:this.data.detail,
          area:this.data.areaChoose,
          location:
          this.data.areaChoose[0].name+''+this.data.areaChoose[1].name+''+this.data.areaChoose[2].name+this.data.detail
        }
      }).then(res=>{
        wx.navigateBack()
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      this.setData({addressId:options.id})
      request({
        url:"/address/detail?id="+this.data.addressId,
      }).then(res=>{
        this.setData({
          name:res.data.name,
          location:res.data.location,
          phone:res.data.phone,
          areaChoose:res.data.area,
          ifDefault:res.data.ifDefault,
          detail:res.data.detail,
        })
      })
    }
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})